
const baseURL = ''

/**
 * api 服务异常
 */
class APTError {
    constructor(code, msg) {
        this.code = code;
        this.msg = msg;
    }
}

class BaseAPI {
  constructor(url) {
    this.url = baseURL + url;
  }

  generateUrl(extraPath = '') {
    if (!this.url) {
      throw new APIError(-1, 'api 方法没有定义url!');
    }
    let queryUrl = '';
    if (extraPath) {
      extraPath = '' + extraPath;
      extraPath = extraPath.startsWith('/') ? extraPath.slice(1) : extraPath;
      queryUrl = this.url + '/' + extraPath;
    } else {
      queryUrl = this.url;
    }
    return queryUrl;
  }

  /**
   * Get One
   * @param {*} id 
   * @param {*} onSuccess 
   * @param {*} onFail 
   */
  getOne(id, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl(id);
    console.log(`>>> Get One url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'GET',
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }

  /**
   * Get Few
   * @param {*} query 
   * @param {*} onSuccess 
   * @param {*} onFail 
   */
  getFew(query = {}, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl();
    console.log(`>>> Get Few url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'GET',
      data: query,
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }

  /**
   * Create
   * @param {*} data 
   * @param {*} onSuccess 
   * @param {*} onFail 
   */
  create(data, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl();
    console.log(`>>> Create One url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'POST',
      data: data,
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }

  /**
   * Create
   * @param {*} id
   * @param {*} data 
   * @param {*} onSuccess 
   * @param {*} onFail 
   */
  modify(id, data, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl(id);
    console.log(`>>> Modify One url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'PUT',
      data: data,
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }
  /**
   * Create
   * @param {*} id 
   * @param {*} onSuccess 
   * @param {*} onFail 
   */
  delete(id, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl(id);
    console.log(`>>> Delete One url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'DELETE',
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }
}


/**
 * 资源状态
 */
class ResourcesAPI extends BaseAPI {
  constructor() {
    super('/bt/resources');
  }
}

/**     
 * 客户端通知
 */
class ClientsNotifyAPI extends BaseAPI {
  constructor() {
    super('bt/clients/notify')
  }

  notify(notifyType, data, onSuccess = $.noop, onFail = $.noop) {
    const queryUrl = this.generateUrl(notifyType);
    console.log(`>>> Notfiy One url: ${queryUrl}`);
    $.ajax({
      url: queryUrl,
      type: 'POST',
      data: data,
      success: function (res) {
        onSuccess(res);
      },
      error: function (error) {
        onFail(error);
      }
    });
  }
}

const innerServices = {
    resourcesAPI: new ResourcesAPI(),
    clientsNotifyAPI: new ClientsNotifyAPI(),
}

